<!doctype html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="com.google.fbyoutube.shared.*"%>
<%@ page import="org.json.JSONObject"%>
<%@ page import="org.apache.commons.codec.binary.Base64" %>
<%@ page import="java.util.*" %>
<%@ page import="java.util.logging.Logger;" %>

<%
	Logger logger = Logger.getLogger(this.getClass().getName());
	// Auth url: https://www.facebook.com/dialog/oauth/?client_id=140444622729093&redirect_uri=https://apps.facebook.com/youtubeonfb/&state=firstlogin&scope=email,read_stream,user_location,publish_stream,publish_actions,manage_pages
	logger.warning(request.toString());
	String signed_request = request.getParameter("signed_request");
	if (signed_request == null) {
		response.sendRedirect(FBUtility.APP_URL + (request.getQueryString() == null ? "" : "?" + request.getQueryString()));
		return;
	} else {
		logger.warning("signed_request = " + signed_request);
		String[] req_section = signed_request.split("\\.");
		req_section[1] = req_section[1].replaceAll("-", "+");
		req_section[1] = req_section[1].replaceAll("_", "/");
		logger.warning("req_section[1] = " + req_section[1]);
		Base64 base64 = new Base64();
		String decodeString = new String(base64.decode(req_section[1].getBytes()));
		logger.warning("decodeString = " + decodeString);
		JSONObject json = new JSONObject(decodeString);
	 	String user_id = json.has("user_id") ? (String) json.get("user_id") : null;
	 	logger.warning("user_id = " + user_id);
	 	if (user_id == null) {
	    	StringBuilder sb = new StringBuilder(FBUtility.DOMAIN_MAP.get(FBUtility.WWW));
	    	sb.append("dialog/oauth/?");
	    	sb.append("client_id=" + FBUtility.APPID);
	    	sb.append("&redirect_uri=" + FBUtility.APP_URL + (request.getQueryString() == null ? "" : "?" + request.getQueryString()));
	    	sb.append("&state=newuser");
	    	sb.append("&scope=email,read_stream,user_location,publish_stream,publish_actions,manage_pages");
	    	logger.warning("sendRedirect = " + sb.toString());
	    	out.println("<script>");
	    	out.println("window.top.location.href = '" + sb.toString() + "'");
	    	out.println("</script>");
	 		//response.sendRedirect(sb.toString()); // redirect not work
	 		return;
	 	}
 	}
 	
 	//response.addHeader("P3P","CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"");
%>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>YouTube Video</title>
    <script type="text/javascript" language="javascript" src="youtube/youtube.nocache.js"></script>
  </head>
  <body>
    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
    <noscript>
      <div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
        Your web browser must have JavaScript enabled
        in order for this application to display correctly.
      </div>
    </noscript>
    
  <div id="fb-root"></div>
  <script src="//connect.facebook.net/en_US/all.js"></script>
  </body>
</html>
